<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');


//首页
class Index extends CT_Controller
{

    public function __construct()
    {
        parent::__construct();
        
        $this->load->model('course_cate_model');
        $this->load->model('course_list_model');
        $this->load->model('course_package_model');
        $this->load->model('order_model');
    }

    /**
     * 首页
     */
    public function index()
    { 
        //园所选择
        if (!$this->_nursery_id){
            redirect('/bind/area');
        }

//         $conditions = array('nursery_id'=>$this->_nursery_id, 'status'=>1);
        $conditions = array('status'=>1);
        $fields = "course_cate.*, (select count(1) from `course_list` where course_list.course_cate_id = course_cate.id) as cl_num ";
        $order_by = 'weight desc,sales desc';
        $data['cc_data'] = $this->course_cate_model->findAll($conditions,$fields, $order_by);
        $data['cp_data'] = array();
        
        //24小时仍未付款的订单变更已过期状态
        $this->order_model->update_order_not_pay();
        
        //我的课包
        if($this->_uid  && !$this->_is_teacher){
            $conditions = array('order.member_id'=>$this->_uid, 
                                'nursery_id'=>$this->_nursery_id,
//                                 'order.status in (1,3,5)'=>NULL
            );
            $fields = "order.*, course_package.title, course_package.logo ";
//             $order_by = 'order.surplus_hours desc, order.pay_time desc'; 
            $order_by = 'order.id desc';
            $offset = 0;
            $limit = 2;
            
            $data['cp_data'] = $this->order_model->get_order_package_list($conditions, $fields, $order_by, $offset, $limit);
        }
        
        
        $this->load->model('area_model');
        $this->load->model('city_model');
        $this->load->model('nursery_model');
        //就近园所
        $conditions = array('id'=>$this->_nursery_id, 'status'=>1);
        $nursery_info = $this->nursery_model->find($conditions);
        $data['nursery_info'] = $nursery_info;
        
        if($nursery_info){
            //当前城市
            $conditions = array('id'=>$nursery_info['city_id'], 'status'=>1);
            $city_info = $this->city_model->find($conditions);
            $data['city_info'] = $city_info;
            //当前地区
            $conditions = array('id'=>$nursery_info['area_id'], 'status'=>1);
            $area_info = $this->area_model->find($conditions);
            $data['area_info'] = $area_info;
        }
        
        $this->ct_template->write('_title', '首页');
        $this->ct_template->write_view('content', 'index/index', $data, FALSE);
        $this->ct_template->render();
    }
    
    /**
     * 课程列表
     */
    public function clist()
    {
        $cc_id = intval($this->input->get('ccid', TRUE));
        if (!$cc_id){
            redirect('/');
        }
        
        $conditions = array('id'=>$cc_id, 'status'=>1);
        $fields = '*';
        $cc_info = $this->course_cate_model->find($conditions,$fields);
        
        if(!$cc_info){
            redirect('/');
        }
        
        $data['cc_info'] = $cc_info;
        
        $conditions = array('course_cate_id'=>$cc_id, 'status'=>1);
        $fields = '*';
        $order_by = 'weight desc';
        $cl_info = $this->course_list_model->findAll($conditions,$fields,$order_by);
        
        $data['cl_info'] = $cl_info;

        $this->ct_template->write('_title', '课程列表');
        $this->ct_template->write_view('content', 'index/clist', $data, FALSE);
        $this->ct_template->render();
    
    }
    
    /**
     * 课程列表详情
     */
    public function cpackage()
    {
        $cl_id = intval($this->input->get('clid', TRUE));
        if (!$cl_id){
            redirect('/');
        }
    
        $conditions = array('id'=>$cl_id, 'status'=>1);
        $fields = '*';
        $cl_info = $this->course_list_model->find($conditions,$fields);
    
        if(!$cl_info){
            redirect('/');
        }
    
        $data['cl_info'] = $cl_info;
    
        $conditions = array('course_list_id'=>$cl_id, 'status'=>1);
        $fields = '*';
        $order_by = 'weight desc';
        $cp_info = $this->course_package_model->findAll($conditions,$fields,$order_by);
    
        $data['cp_info'] = $cp_info;
        $data['cp_evaluate_info'] = array();
        
        if($cp_info){
            //用户评价
            $course_package_ids = '';
            foreach ($cp_info as $k=>$v){
                $course_package_ids .= $v['id'].',';
            }
            $course_package_ids = trim($course_package_ids, ',');
            
            $this->load->model('book_evaluate_model');
            $conditions = array("course_package_id in ({$course_package_ids})"=>NUll, 'status'=>1);
            $fields = '*';
            $order_by = 'id desc';
            $cp_evaluate_info = $this->book_evaluate_model->findAll($conditions,$fields,$order_by,0,2);
            
            $data['cp_evaluate_info'] = $cp_evaluate_info;
        }
    
        $this->ct_template->write('_title', '课程详情');
        $this->ct_template->write_view('content', 'index/cplist', $data, FALSE);
        $this->ct_template->render();
    
    }
    
    /**
     * 课程列表详情
     */
    public function cplist_evaluate()
    {
        $cl_id = intval($this->input->get('clid', TRUE));
        if (!$cl_id){
            redirect('/');
        }
        
        $data['cp_info'] = $data['cp_evaluate_info'] = array();
    
        $conditions = array('course_list_id'=>$cl_id, 'status'=>1);
        $fields = 'id';
        $cp_info = $this->course_package_model->findAll($conditions,$fields);
        
        if($cp_info){
            $data['cp_info'] = $cp_info;
            
            //用户评价
            $course_package_ids = '';
            foreach ($cp_info as $k=>$v){
                $course_package_ids .= $v['id'].',';
            }
            $course_package_ids = trim($course_package_ids, ',');
            
            $this->load->model('book_evaluate_model');
            $conditions = array("course_package_id in ({$course_package_ids})"=>NUll, 'status'=>1);
            $fields = '*';
            $order_by = 'id desc';
            $offset = 0;
            $limit = 0;
            $cp_evaluate_info = $this->book_evaluate_model->findAll($conditions,$fields,$order_by,$offset,$limit);
            
            $data['cp_evaluate_info'] = $cp_evaluate_info;
        }
    
        $this->ct_template->write('_title', '课程详情-用户评价');
        $this->ct_template->write_view('content', 'index/cplist_evaluate', $data, FALSE);
        $this->ct_template->render();
    
    }

    
    /**
     * 课程列表详情
     */
    public function cplist_detail()
    {
        $cl_id = intval($this->input->get('clid', TRUE));
        if (!$cl_id){
            redirect('/');
        }
    
        $conditions = array('id'=>$cl_id, 'status'=>1);
        $fields = '*';
        $cl_info = $this->course_list_model->find($conditions,$fields);
        if(!$cl_info){
            redirect('/');
        }
    
        $data['cl_info'] = $cl_info;
    
        $this->ct_template->write('_title', '课程详情-查看详情');
        $this->ct_template->write_view('content', 'index/cplist_detail', $data, FALSE);
        $this->ct_template->render();
    
    }
    
}
